package cm.tl.pms.util;

import java.util.HashMap;
import java.util.Map;

public class NumberToChinese {

    private static final Map<Integer, String> numberToChineseMap = new HashMap<>();

    static {
        numberToChineseMap.put(0, "零");
        numberToChineseMap.put(1, "一");
        numberToChineseMap.put(2, "二");
        numberToChineseMap.put(3, "三");
        numberToChineseMap.put(4, "四");
        numberToChineseMap.put(5, "五");
        numberToChineseMap.put(6, "六");
        numberToChineseMap.put(7, "七");
        numberToChineseMap.put(8, "八");
        numberToChineseMap.put(9, "九");
        numberToChineseMap.put(10, "十");
        numberToChineseMap.put(100, "百");
        numberToChineseMap.put(1000, "千");
        numberToChineseMap.put(10000, "万");
        numberToChineseMap.put(100000000, "亿");
    }

    /**
     * 将 int 数字转换为大写数字
     *
     * @param number 数字
     * @return 大写数字
     */
    public static String toChinese(int number) {
        if (number < 0) {
            throw new IllegalArgumentException("仅支持非负整数");
        }

        if (number < 11) {
            return numberToChineseMap.get(number);
        }

        StringBuilder result = new StringBuilder();
        int unit = 1;
        while (number > 0) {
            int digit = number % 10;
            if (digit != 0) {
                if (unit > 1) {
                    result.insert(0, numberToChineseMap.get(unit));
                }
                result.insert(0, numberToChineseMap.get(digit));
            } else if (result.length() > 0 && result.charAt(0) != '零') {
                result.insert(0, "零");
            }
            number /= 10;
            unit *= 10;
        }

        return result.toString().replaceAll("零+$", "");
    }

    public static void main(String[] args) {
        int number = 12345;
        String chineseNumber = toChinese(number);
        System.out.println(number + " 的大写数字是: " + chineseNumber);
    }
}